<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * @author vitalify
 *
 */
class ObjectMapDB extends CI_Model {
	protected $tableName = '';
	protected $isDebug = false;

	public function __construct($tableName) {
		parent::__construct();
		// load
		$this->load->database(ACTIVE_GROUP);
		$this->tableName = $tableName;
	}
	
	/*
	 * @description: get next, previous id of input id
	 */
	public function get_next_pre($id,$fk_menu_id)
	{
		if( $id && $fk_menu_id ){
			$pre = $next = false;
			//Get previous id
			$query = $this->db->from($this->tableName)
					 ->where('id <'.$id)
					 ->where('fk_menu_id',$fk_menu_id)
					 ->order_by('id','desc')
					 ->limit(1)
					 ->get();
			if($query->num_rows() > 0)
				$pre = $query->result_array();

			//Get next id
			$query = $this->db->from($this->tableName)
					 ->where('id >'.$id)
					 ->where('fk_menu_id',$fk_menu_id)
					 ->limit(1)
					 ->get();
			if($query->num_rows() > 0)
				$next = $query->result_array();
				
			//Return result
			$result = array('next'=>$next[0]['id'],'pre'=>$pre[0]['id']);
			return $result;			
		}				
		return false;
	}	


	/**
	 * @author DatDM
	 * @version 15/03/2013
	 * @return id | FALSE
	 * @param array $input
	 */
	public function insert($input){
		if(is_array($input)){
			//true | false
			$res = $this->db->insert($this->tableName, $input);
			if($this->isDebug) {
				$this->printDebug($this->db->last_query());
			}
			if($res) {
				return $this->db->insert_id();
			}
		}
		return false;
	}
	
	/**
	 * @author DatDM
	 * @version 15/03/2013
	 * @return id | FALSE
	 * @param array $input
	 */
	public function insert_batch($input){
		if(is_array($input)){
			//true | false
			$res = $this->db->insert_batch($this->tableName, $input);
			if($this->isDebug) {
				$this->printDebug($this->db->last_query());
			}
			if($res) {
				return $this->db->insert_id();
			}
		}
		return false;
	}	

	/**
	 * @author DatDM
	 * @version 15/03/2013
	 * @return TRUE | FALSE
	 * @param array $input
	 * @param array $where
	 */
	public function update($input, $where) {
		if( $input && is_array($where) ){
			//true | false
			$res = $this->db->update($this->tableName, $input, $where);
			if($this->isDebug) {
				$this->printDebug($this->db->last_query());
			}
			return $res;
		}
		return FALSE;
	}

	/**
	 * @author DatDM
	 * @version 15/03/2013
	 * @return TRUE | FALSE
	 * @param array $where
	 */
	public function delete($where) {

		if(is_array($where)){
			$res = $this->db->delete($this->tableName, $where);
			if($this->isDebug) {
				$this->printDebug($this->db->last_query());
			}
			return $res;
		}
		return FALSE;
	}

	/**
	 * @author DatDM
	 * @version 15/03/2013
	 * @param int $id
	 * @param string $fields optional
	 * @return false || array 2D
	 */
	public function getById($id, $fields = "*") {
		if($id)
		{
			$this->db->select($fields);
			$this->db->from($this->tableName);
			$this->db->where("id", $id);
			$query = $this->db->get();
			if($this->isDebug) {
				$this->printDebug($this->db->last_query());
			}
			if($query->num_rows() > 0){
				return $query->result_array();
			}
		}

		return false;

	}

	/**
	 * @author DatDM
	 * @version 15/03/2013
	 * @param array $where
	 * @param string $fields optional
	 * @return false || array 2D
	 */

	public function get($where = '', $fields = "*",$order_by = 'id') {
		if(is_array($where)){
			$this->db->select($fields);
			$this->db->from($this->tableName);
			$this->db->where($where);
			$this->db->order_by($order_by,'desc');
			$query = $this->db->get();
		}
		else{
			$this->db->select($fields);
			$this->db->from($this->tableName);
			$this->db->order_by($order_by,'desc');
			$query = $this->db->get();
		}

		if($this->isDebug) {
			$this->printDebug($this->db->last_query());
		}

		if($query->num_rows() > 0){
			return $query->result_array();
		}
		return false;
	}

	/**
	 * @author ChinhLD
	 * @version 15/03/2013
	 * @param array $column
	 * @return false || array 2D
	 */
	public function get_max($column,$where=''){
		if($column){
			if(isset($where) && is_array($where)){
				$query = $this->db->select_max($column)
								  ->where($where)
								  ->get($this->tableName);				
			}
			else{
				$query = $this->db->select_max($column)
							  	  ->get($this->tableName);				
			}
			if($query->num_rows() > 0){
				$rs = $query->result_array();
				return $rs[0][$column];
			}
		}
		return false;
	}

	/**
	 * @author DatDM
	 * @version 15/03/2013
	 * @return array 2D | FALSE
	 */
	public function getFullColumns(){
		$query = $this->db->query("SHOW FULL COLUMNS FROM `" . $this->tableName ."`");
		//Field Type Collation Null Key Default Extra Privileges Comment
		if($this->isDebug) {
			$this->printDebug($this->db->last_query());
		}
		if($query->num_rows() > 0){
			return $query->result_array();
		}
		return false;
	}

	/**
	 * @author DatDM
	 * @version 15/03/2013
	 * @todo map field match filed in DB
	 * @param string $string: id,name,...
	 * @return boolean
	 */
	public function validFields($string) {
		$list = explode(',', $string);
		$ret = $this->getFullColumns();
		// 		echo '<pre>';
		// 		print_r($ret);
		// 		echo '</pre>';
		// 		die;
		foreach ($ret AS $v1) {
			foreach ($list as $v2) {
				$temp1 = strtolower(trim($v1['Field']));
				$temp2 = strtolower(trim($v2));
				if( $temp1 == $temp2 ) {
					return true;
				}
			}
		}
		return false;
	}

} // end class ObjectMapDB
